import 'dart:typed_data';

class VirtualOffset implements Comparable {
  num blockPosition;
  num dataPosition;

  // < offset of the compressed data block
  // < offset into the uncompressed data
  VirtualOffset(this.blockPosition, this.dataPosition);

  toString() {
    return '${this.blockPosition}:${this.dataPosition}';
  }

  @override
  int compareTo(other) {
    int del = this.blockPosition - other.blockPosition;

    return del != 0 ? del : this.dataPosition - other.dataPosition;
  }

  @override
  bool operator ==(Object other) => identical(this, other) || other is VirtualOffset && runtimeType == other.runtimeType && blockPosition == other.blockPosition && dataPosition == other.dataPosition;

  @override
  int get hashCode => blockPosition.hashCode ^ dataPosition.hashCode;

  min(List<VirtualOffset> args) {
    var min;
    var i = 0;
    for (; !min; i += 1) min = args[i];
    for (; i < args.length; i += 1) {
      if (min.compareTo(args[i]) > 0) min = args[i];
    }
    return min;
  }

  static VirtualOffset fromBytes({Uint8List bytes, offset = 0, bigEndian = false}) {
    if (bigEndian) throw Exception('big-endian virtual file offsets not implemented');
    return VirtualOffset(
      bytes[offset + 7] * 0x10000000000 + //
          bytes[offset + 6] * 0x100000000 +
          bytes[offset + 5] * 0x1000000 +
          bytes[offset + 4] * 0x10000 +
          bytes[offset + 3] * 0x100 +
          bytes[offset + 2],
      (bytes[offset + 1] << 8) | bytes[offset],
    );
  }
}
